<?php

namespace App\Jobs;

use App\Events\Refund as RefundEvents;
use App\Models\Refund as RefundModel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

use App\Services\PosApi\RefundConfirm;

class RefundJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $orderInfo;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($orderInfo)
    {
        $this->onConnection('redis-enhanced');
        $this->orderInfo = $orderInfo;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        event(new RefundEvents($this->orderInfo));
        sleep(3);
        //更新主退款单状态
        $refund = (new RefundModel())->updateRefundStatus($this->orderInfo['refunds_id']);
        //通知后端
        if($refund->status===1){
            $rcService = new RefundConfirm();
            $rcService->handle($refund);
        }

    }
}
